package rgbos.forum.search;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import rgbos.forum.domain.Post;

public class SearchAgent implements Runnable {
	
	public BlockingQueue<Post> postsQueue_;
	private SearchIndex searchIndex_;
	
	public SearchAgent(SearchIndex searchIndex) {
		super();
		postsQueue_ = new LinkedBlockingQueue<Post>();
		searchIndex_ = searchIndex;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Post tPost = postsQueue_.take();
				searchIndex_.addPostToIndex(tPost);
			} catch (InterruptedException e) {
				//just try again
			}			
		}
	}
	
	public void indexPost(Post message) {
		postsQueue_.add(message);
	}
}
